package com.itheima.tlias.controller;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias.common.result.Result;
import com.itheima.tlias.config.StudentConfig;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/yml")
public class TestResdYmlController {
    @Value("${student.name}")
    String name;
    @Value("${student.age}")
    Integer age;
    @Value("${student.subject}")
    String[] subject;

    @GetMapping("/value")
    public Result readCustomConfigYmlByValue() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",name );
        jsonObject.put("age", age);
        jsonObject.put("subject", subject);
        jsonObject.put("teachers", "@Value注解不支持映射list复杂属性");
        jsonObject.put("family", "@Value注解不支持映射map复杂属性");
        return Result.success(jsonObject);
    }
    @Resource
    private StudentConfig config;

    @GetMapping("/config")
    public Result readCustomConfigYmlByConfig() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",config.getName() );
        jsonObject.put("age", config.getAge());
        jsonObject.put("subject", config.getSubject());
        jsonObject.put("teachers", config.getTeachers());
        jsonObject.put("family", config.getFamily());
        return Result.success(jsonObject);
    }
    @Resource
    Environment env;
    @GetMapping("/env")
    public Result readCustomConfigYmlByEvn() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",env.getProperty("student.name"));
        jsonObject.put("age", env.getProperty("student.age"));
        jsonObject.put("subject", env.getProperty("student.subject"));
        jsonObject.put("teachers", env.getProperty("student.teachers"));
        jsonObject.put("family", env.getProperty("student.family"));
        String[] activeProfiles = env.getActiveProfiles();
        jsonObject.put("获取当前活动的配置文件名称数组", List.of(activeProfiles));
        return Result.success(jsonObject);
    }

}
